#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    init();
    this->setFixedSize(750,550);
}

Widget::~Widget()
{
    delete ui;
}
void Widget::on_pushButton_2_clicked()  // close btn clicked
{
    this->close();

}

void Widget::on_pushButton_3_clicked()  //find path btn clicked
{
    QString chosingPath = QFileDialog::getExistingDirectory(this,"选择路径");
    if(chosingPath.isEmpty())
        return;
    else
        ui->lineEdit_13->setText(chosingPath);
}

void Widget::init()
{
    inputLimit();
}

void Widget::inputLimit(){
    QRegExpValidator *Reva1 = new QRegExpValidator(QRegExp("[0-9]{10}"),this);                  //只能输数字，最多十位
    ui->lineEdit->setValidator(Reva1);
    ui->lineEdit_12->setValidator(Reva1);
    QRegExpValidator *Reva2 = new QRegExpValidator(QRegExp("[0]+([.]{1}[1-8]){0,1}"),this);     //只能输一位小数0.1-0.8
    ui->lineEdit_2->setValidator(Reva2);
    QRegExpValidator *Reva3 = new QRegExpValidator(QRegExp("[4-9]|10"),this);                   //只能4-10
    ui->lineEdit_3->setValidator(Reva3);
    QRegExpValidator *Reva4 = new QRegExpValidator(QRegExp("[0-9.0-9]*"),this);                 //只能数字或者小数 没有限制多个小数点
    ui->lineEdit_4->setValidator(Reva4);
    ui->lineEdit_7->setValidator(Reva4);
    QRegExpValidator *Reva5 = new QRegExpValidator(QRegExp("[0]+(.[0][1-4]){1}"),this);         //0.01-0.04 only
    ui->lineEdit_9->setValidator(Reva5);
    QRegExpValidator *Reva6 = new QRegExpValidator(QRegExp("[0]+(.[0][2-9][0-9]){2}"),this);    //bigger than 0.02 only
    ui->lineEdit_8->setValidator(Reva6);
    QRegExpValidator *Reva7 = new QRegExpValidator(QRegExp("[0]+(.[0][8-9][0-9]){2}"),this);    //bigger than 0.08 only
    ui->lineEdit_10->setValidator(Reva7);
    QRegExpValidator *Reva8 = new QRegExpValidator(QRegExp("[0]+(.[0][4-9][0-9]){2}"),this);    //bigger than 0.08 only
    ui->lineEdit_11->setValidator(Reva8);
    return ;
}

bool  Widget::inputCheck(){
    bool invalid = false;
    if(!(num && F && a && b && Thickness1 && Thickness2 && TopLength  && TopWidth && BottomWidth)){
        qDebug()<<"sxs";
        QMessageBox::critical(this,"错误","输入有误,请检查！");
        invalid = true;
    }
    if(!ui->lineEdit_13->text().size()){
        QMessageBox::critical(this,"错误","请输入路径!");
        invalid = true;
    }
   return invalid?false:true;
}

void Widget::dataTrans(){
    num = ui->lineEdit->text().toInt();
    F = ui->lineEdit_12->text().toDouble();
    a = ui->lineEdit_2->text().toDouble();
    b = ui->lineEdit_3->text().toDouble();
    Thickness1 = ui->lineEdit_4->text().toDouble();
    Thickness2 = ui->lineEdit_7->text().toDouble();
    TopLength = ui->lineEdit_9->text().toDouble();
    TopWidth = ui->lineEdit_8->text().toDouble();
    BottomLength = ui->lineEdit_10->text().toDouble();
    BottomWidth = ui->lineEdit_11->text().toDouble();
    path = ui->lineEdit_13->text()+QString("\\RIB_APDL.txt");
    return ;
}

void Widget::fileWriter(){
    QFile file(path);
    if(!file.open(QFileDevice::WriteOnly|QIODevice::Text)){
        qWarning()<<"open file failed! path = "<<path;
        return ;
    }
    qDebug()<<"open "<<path<<" sucess";
    QString head_text = "/FILNAM, EX01     !定义文件名\r\n"
                   "/TITLE, CANTILEVERBEAM DEFECTION\r\n"
                   "/ UNITS , SI!定义单位制\r\n"
                   "/ PREP7!进入前置处理\r\n"
                   "ET, 1, 188!定义单元类型为beam188\r\n";
    file.write(head_text.toLocal8Bit());

    QVector<double> Coordinate_x,Coordinate_y;
    Coordinate_x.append(0); //无意义填充
    Coordinate_y.append(0);
    double f = b * 3 / 8;
    for(int i = 1;i <= num;i++){
        Coordinate_x.append(a+(b-a)/num*i);
        Coordinate_y.append(Coordinate_x.at(i)*Coordinate_x.at(i)/4/f);
    }
    for(int i = 1; i < num;i++){
        QString text_1 = QString("\r\nSECTYPE,%1,BEAM,HREC,0  !空心矩形截面,%2\r\n"
                       "SECDATA,%3,%4,%5,%6,%7,%8,0, 0, 0, 0!截面%9,尺寸定义")
                .arg(i).arg(i).arg((BottomLength - (BottomLength - TopLength) / (num - 1) * i))
                .arg((BottomWidth - (BottomWidth - TopWidth) / (num - 1) * i))      //arg4
                .arg(Thickness1).arg(Thickness1)    //arg6
                .arg(Thickness2).arg(Thickness2).arg(i);
        file.write(text_1.toLocal8Bit());
    }
    file.write("\r\nMP, EX, 1, 210E9!定义杨氏模量\r\n"
               "MP, PRXY, 1, 0.3!定义泊松比\r\n");
    for(int i = 1;i <= num;i++){
        QString text_2 = QString("N,%1,%2,%3,0 !定义节点\r\n").arg(i).arg(Coordinate_x.at(i)).arg(Coordinate_y.at(i));
        file.write(text_2.toLocal8Bit());
    }
    for(int i = 1;i< num;i++){
        QString text_3 = QString("SECNUM,%1,\r\nE,%2,%3\r\n").arg(i).arg(i).arg(i+1);
        file.write(text_3.toLocal8Bit());
    }
    file.write(" FINISH!退出该处理层\r\n/SOLU!进入求解处理器\r\nD, 1, ALL, 0! 对1 节点施加约束使它X ， Y向位移都为0\r\n");
    for(int i = 1;i <= num;i++){
        QString text_4 = QString("SFBEAM,%1,2,PRES,%2!在i 号单元的第2个面上施加压力\r\n").arg(i).arg(-F);
        file.write(text_4.toLocal8Bit());
    }
    file.write(" SOLVE! 计算求解\r\n"
               "FINISH!完成该处理层\r\n"
               "/ POST1! 进入后处理\r\n"
               "SET, 1, 1! 查看子步\r\n"
               "PLDISP! 显示变形后的形状\r\n"
               "* CFCLOSE\r\n"
               "FINISH!完成");
    file.close();
    QMessageBox::StandardButtons msgDlg = QMessageBox::information(nullptr,"提示","写入成功!");
    return;
}
void Widget::on_pushButton_clicked()    //Create txt btn pressed
{
    dataTrans();
    if(!inputCheck()) return;
    fileWriter();   //同步IO操作，有可能阻塞
}



